package com.lavida.project.system.emp.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.aspectj.lang.annotation.Log;
import com.lavida.framework.aspectj.lang.enums.BusinessType;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.framework.web.page.TableDataInfo;
import com.lavida.project.system.emp.domain.Emp;
import com.lavida.project.system.emp.domain.Empleave;
import com.lavida.project.system.emp.domain.Roster;
import com.lavida.project.system.emp.service.EmpService;
import com.lavida.project.system.emp.service.EmphistoryService;
import com.lavida.project.system.emp.service.EmpleaveService;
import com.lavida.project.system.emp.service.EmpqtService;
import com.lavida.project.system.emp.service.RosterService;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.service.IUserService;
import com.sun.jna.platform.win32.WinUser.MSG;
@Controller
@RequestMapping("system/emp1")
public class RosterController extends BaseController{
	private String prefix = "system/emp";
	@Autowired
	private RosterService rosterService;
	@Autowired
	private IUserService userService;
	@Autowired
	private EmpqtService empqtService;
	@Autowired
	private EmphistoryService emphistoryService;
	@Autowired
	private EmpService empServive;
	@Autowired
	private EmpleaveService empleaveService;
	@RequiresPermissions("company:roster:view")
	@RequestMapping()
	public String emp(ModelMap mmap) {
		User user  = ShiroUtils.getSysUser();
		User user1 = userService.selectUserById(user.getUserId());
		mmap.put("user", user1);
		return prefix + "/roster";
	}
	@RequiresPermissions("company:roster:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Roster roster) {
		startPage();
		List<Roster> selectRoster = rosterService.selectRoster(roster);
		return getDataTable(selectRoster);
	}
	@RequestMapping("/add")
	public String add(ModelMap mmap) {
		return prefix + "/add";
	}
	@RequiresPermissions("company:roster:add")
	@Log(title = "参数管理", businessType = BusinessType.INSERT)
	@PostMapping("/showAdd")
	@ResponseBody
	public Map<String,Object> showAdd(Roster roster) {
		//System.out.println(">>>>>>>>>>>>>>>>>>>>>"+roster.getCompany());
		Map<String, Object> result = new HashMap<String,Object>();
		String msg = "";
		String code = "";
		Empleave empleave = new Empleave();
		empleave.setJobId(Integer.valueOf(roster.getJob_id()));
		empleave.setName(roster.getName());
		empleave.setYwjiao("业务文件交接");
		empleave.setGzquntc("工作群退出");
		empleave.setLzyxtzhi("离职邮件通知");
		empleave.setJkjjie("借款交接");
		empleave.setGzzjiaojie("工作证交接");
		empleave.setYgscjiaojie("员工手册交接");
		empleave.setKqgb("考勤关闭");
		empleave.setDdgb("钉钉关闭");
		empleave.setGxgb("共享关闭");
		empleave.setYxgb("邮箱关闭");
		empleave.setBxgb("报销关闭");
		empleave.setDnzh("电脑账号关闭");
		empleave.setXtgb("系统关闭");
		empleave.setBgyb("办公用品交接");
		empleave.setYsjiaojie("钥匙交接");
		empleave.setGsquntc("公司群退出");
		empleave.setWbzhjj("外部账号交接");
		empleave.setWbzhzx("外部账号注销");
		empleave.setGzff("工资发放");
		empleave.setLdsc("劳动手册");
		empleave.setTgd("退工单");
		empleave.setZlbf("资料备份");
		int i = empleaveService.insertEmpleave(empleave);
		if(i == 1) {
			int j = empServive.insertEmpMessage(roster);
			if (j==1) {
				rosterService.insertRoster(roster);
				msg = "成功";
				code = "0";
			}
		}
		result.put("msg", msg);
		result.put("code", code);
		return result;
		/*return "redirect:/system/emp1";*/
	}
	@RequiresPermissions("company:roster:delete")
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,String> delete(@RequestParam("job_ids[]")int[] job_ids) {
		Map<String, String> result = new HashMap<String,String>();
		rosterService.deleteRoster(job_ids);
		empServive.deleteEmpMesssage(job_ids);
		emphistoryService.deleteEmphistoryj(job_ids);
		empqtService.deleteEmpqtj(job_ids);
		empleaveService.deleteEmpleave(job_ids);
		result.put("msg", "1");
		return result;
	}
	@RequestMapping("/getRoster")
	@ResponseBody
	public Map<String,Object> getRoster(int job_id) {
		Map<String,Object> result = new HashMap<String,Object>();
		Roster roster = rosterService.getRoster(job_id);
		result.put("roster", roster);
		return result;
	}
	@RequestMapping("/getRostercard")
	@ResponseBody
	public boolean getRostercard(String id_card) {
		Map<String,Object> result = new HashMap<String,Object>();
		Roster roster =	rosterService.selectRosterscard(id_card);
		int msg = 0;
		if(roster!=null) {
			return false;
		}
		/*System.out.println("msg:"+msg);
		result.put("msg", msg);*/
		return true;
	}
	@RequestMapping("/getRosterJOb")
	@ResponseBody
	public boolean getRostJOb(Integer job_id) {
		Map<String,Object> result = new HashMap<String,Object>();
		Roster roster = rosterService.getRoster(job_id);
		int msg = 0;
		if(roster!=null) {
			return false;
		}
		/*System.out.println("msg:"+msg);
		result.put("msg", msg);*/
		return true;
	}
	@RequiresPermissions("company:roster:edit")
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@RequestMapping("/upadteRoster")
	@ResponseBody
	public Map<String, Object> upadteRoster(Roster roster) {
		Map<String,Object> result = new HashMap<String,Object>();
		String msg = "";
		String code = "";
		try {
			int j = empServive.updateEmpMessage(roster);
			int a = rosterService.updateRoster(roster);
			Empleave empleave = new Empleave();
			empleave.setName(roster.getName());
			empleave.setJobId(Integer.valueOf(roster.getJob_id()));
			empleaveService.updateEmpleavebfs(empleave);
		}catch (Exception e) {
			// TODO: handle exception
			result.put("msg", e);
			result.put("code", "0");
		}
		result.put("code", "0");
		result.put("msg","数据已成功");
		return result;
		/*return "redirect:/system/emp1";*/
	}
}
